package cn.edu.scau.cmi.suyaoxing.client;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.scau.cmi.suyaoxing.composite.consistent.*;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


public class ConsistentCompositeClient extends Application{
   public static void main(String[] args) {
       launch(args);
    }
   private String[] type= {"组织","个体"};
   private int num;
   private int countLeaf=0;
   private int countComposite=0;
   ConsistentComposite composite1=new ConsistentComposite(countComposite++);
   ConsistentLeaf leaf1=new ConsistentLeaf(countLeaf++);
   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
   @Override
   public void start(Stage primaryStage) {
       GridPane gridpane=new GridPane();
       gridpane.setAlignment(Pos.CENTER);
       gridpane.setHgap(1);
       gridpane.setVgap(1);
       
       ComboBox<String> cbo=new ComboBox<>();
       ObservableList<String> items=FXCollections.observableArrayList(type);
       cbo.getItems().addAll(items);
       
       Label lb1=new Label("要删除节点的序号:");
       TextField tf1=new TextField();
       TextArea ta=new TextArea();
       Button bt1=new Button("添加个体");
       Button bt2=new Button("添加组织");
       Button bt3=new Button("删除1个节点");
       Button bt4=new Button("输出所有子节点");
       //添加Leaf型节点
       bt1.setOnAction(new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent event) {
        	   num=items.indexOf(cbo.getValue());
        	   switch(num) {
        	   case 0:
        		   ConsistentComponent leaf=new ConsistentLeaf(countLeaf++);
            	   composite1.addChild(leaf);
            	   ta.setText("添加  "+leaf.toString());
        	       break;
        	   case 1:
        		   ta.setText("\"叶子节点\"无法添加子节点");
        	       break;
        	       default:ta.setText("请选择节点类型");
        	   }
        	   
           }
       });
       //添加Composite型节点
       bt2.setOnAction(new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent event) {
        	   
        	   num=items.indexOf(cbo.getValue());
        	   switch(num) {
        	   case 0:
        		   ConsistentComponent composite=new ConsistentComposite(countComposite++);
            	   composite1.addChild(composite);
            	   ta.setText("添加  "+composite.toString());
        	       break;
        	   case 1:
        		   ta.setText("\"叶子节点\"无法添加节点");
        	       break;
        	       default:ta.setText("请选择节点类型");
        	   }
           }
       });
       //删除节点，序号从0开始
       bt3.setOnAction(new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent event) {
        	   num=items.indexOf(cbo.getValue());
        	   switch(num) {
        	   case 0:
        		   if(ta.getText().trim().isEmpty()) {
            		   ta.setText("请输入要删除的节点序号:");
            	   }
            	   else {
            		   int index=Integer.parseInt(tf1.getText());
            		   if(index>=0&&index<composite1.getSize()) {
            			   composite1.removeChild(index);
            			   ta.setText("已删除第"+index+"号节点--"+df.format(new Date()));
            		   }
                    	   else
                    		   ta.setText("你输入的数值超出范围");
            	   }
        	       break;
        	   case 1:
        		   ta.setText("叶子节点无法删除节点");
        	       break;
        	       default:ta.setText("请选择节点类型");
        	   }
           }
       });
       //输出所有的节点
       bt4.setOnAction(new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent event) {
        	   num=items.indexOf(cbo.getValue());
        	   switch(num) {
        	   case 0:
        		   ta.clear();
        		   ta.appendText("当前根节点："+composite1.toString()+"\n");
            	   for(int i=0;i<composite1.getList().size();i++) {
            		   ta.appendText(composite1.getChild(i).toString()+"\n");
            	   }
        	       break;
        	   case 1:
        		   ta.clear();
        		   ta.appendText("当前根节点："+leaf1.toString()+"\n");
        	       break;
        	       default:ta.setText("请选择节点类型");
        	   }
           }
       });
       
       gridpane.add(bt1, 0, 0,1,1);
       gridpane.add(bt2, 1, 0,1,1);
       gridpane.add(bt3, 2, 0,1,1);
       gridpane.add(bt4, 3, 0,1,1);
       gridpane.add(lb1, 0, 1,1,1);
       gridpane.add(tf1, 1, 1,1,1);
       gridpane.add(ta, 0, 3, 5, 1);
       gridpane.add(cbo, 2, 1,1,1);
       Scene scene=new Scene(gridpane,800,400);
       primaryStage.setTitle("ConsistentComponent");
       primaryStage.setScene(scene);
       primaryStage.show();
    }
}